package com.zookeeper.t4;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 作者: LCG
 * 日期: 2020/7/14 21:18
 * 描述:
 */
public class ZkLock {

    public static void main(String[] args) throws Exception {

        //尝试策略
        RetryPolicy retryPolicy=new ExponentialBackoffRetry(1000,3);

        //创建连接
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("192.168.137.1:2181", retryPolicy);

        //开启链接
        curatorFramework.start();

        //进程互斥
        InterProcessMutex interProcessMutex=new InterProcessMutex(curatorFramework,"/LOCK/LC_");

        //获取锁
        interProcessMutex.acquire();

        System.out.println("====================");

        //释放锁
        interProcessMutex.release();


    }

}
